--
-- Created by IntelliJ IDEA.
-- User: WenYF
-- Date: 2019/1/6 0006
-- Time: 下午 2:53
-- To change this template use File | Settings | File Templates.
-- 中队。军队中的中队，所有行动单元都属于某个中队
local SquadIndex = 0
local Squad = {
    id = nil,
    name = "中队",
    captain = nil,                  -- 队长
    units = Set(),                  -- 战斗单元
    formation = nil,                -- 阵型
    arms = "changqiang",            -- 兵种
    level = 1,                      -- 兵种等级
    number = 0,                     -- 士兵上线数量
    movePosition = nil,             -- 移动到哪里
    inFormation = true,             -- 是否保持阵型
    inStand = true,                 -- 是否待命
    inFreeAttack = false,           -- 是否自由战斗
    moveSpeed = -1,
}


function Squad:create(captainUnit, units)
    local squad = clone(Squad)
    squad.captain = captainUnit
    squad.units:insert(captainUnit)
    if units ~= nil then
        for k,unit in pairs(units) do
            squad.units:insert(unit)
            unit.squad = squad
            print(unit.name .. "设置中队---------------------")
        end
    end

    squad:init()
    return squad
end

function Squad:init()
    -- TODO 初始化阵型结构
    self.inFormation = true
    self.inStand = true
    self.inFreeAttack = false

    self:setFormation("quene1")
    self:findSlowestMoveSpeed()
    --self:keepFormation()
end

-- 找到最慢速度单元
function Squad:findSlowestMoveSpeed()
    for k, unit in pairs(self.units) do
        local speed = unit:getMoveSpeed()
        if self.moveSpeed == -1 then
            self.moveSpeed = speed
        end
        if self.moveSpeed > speed then
            self.moveSpeed = speed
        end
    end
end

-- 移动到
function Squad:move(p1, p2) 
    if type(p1) == "table" then
        self.movePosition = p1
    else
        self.movePosition = {x=p1,x=p2}
    end
    self.inStand = false
end


-- 设置阵型
function Squad:setFormation(formationId)
    if self.formation and self.formation.id == formationId then
        return    
    end
    
    self.formation = Formation:getFormation(formationId)
end

-- 保持阵型
function Squad:keepFormation()
    if self.formation == nil then
        return
    end
    --self.movePosition = nil
    logW("[" .. self.name .. "] 开始保持队列站位")
    -- 调整站位
    self:setUnitStanceInFormation()
end

-- 设置单元的站位为阵列
function Squad:setUnitStanceInFormation()
    Formation:standUnits(self.captain.position, self.units, self.formation, 30 * math.pi / 180)
end

-- 自由战斗
function Squad:freeAttack()
    -- 给unit设置为自由战斗
    self.inFreeAttack = true
    for k,unit in pairs(self.units) do
        unit:freeAttack()    
    end
end

-- 添加一个单元
function Squad:add(unit) 
    if self.units:find(unit) == true then
        return
    end
    self.units:insert(unit)
    unit.squad = squad
    self:keepFormation()
end

function Squad:standOrder()
    self.inStand = true
    self.movePosition = nil
end

function Squad:moveOrder(position)
    logW("[" .. self.name .. "] 移动到 " .. position.x .. "," .. position.y, self.moveSpeed)
    self:move(position)
end

function Squad:process(dt, ct, ci)
    local captain = self.captain
    logW("[" .. self.name .. "] process")
    if self.inFormation == true and self.movePosition ~= nil and captain ~= nil and self.moveSpeed > 0 then
        local xDistance, yDistance
        local dist = dt * self.moveSpeed
    
        if dist > distance(captain.position, self.movePosition) then
            xDistance = self.movePosition.x - captain.position.x
            yDistance = self.movePosition.y - captain.position.y
        else
            -- 得到距离
            local ang = angle(captain.position, self.movePosition)
            -- 得到地形影响 
            xDistance = dt * self.moveSpeed * math.cos(ang/180 * 3.1415)
            yDistance = dt * self.moveSpeed * math.sin(ang/180 * 3.1415)
        
            if xDistance == 0 and yDistance == 0 then
                return
            end
        end
        for k,unit in pairs(self.units) do
            unit:moveBy(xDistance, yDistance)
        end
    end

    for k,unit in pairs(self.units) do
        unit:process(dt, ct, ci)
    end
end

return Squad
